Call Stack vs Memory Heap

Stack vs Heap Memory - Simple Explanation - YouTube
https://www.youtube.com/watch?v=5OJRqkYbK-4

Transcript:
(00:00) Hey friends, welcome back to the channel. Today we're going to have a quick look at stack and heap memory and see how your code and your variables are stored in memory when your application is running. Now, getting a good understanding of what your code is doing in memory can be really helpful to work out why things have different scopes.
(00:16) Now the memory of your application is stored in three main parts. The first part is machine code. This is where your application is converted into instructions that your computer can understand. The next two parts are the stack and the heap. Now to understand the stack, you need to understand the stack data structure.
(00:31) Now the stack data structure is a little bit like a stack of books, so you imagine you've got a book stacked like this. You can take things off the top of the stack and you can put things on the top of the stack, but you can't access anything for the remainder of the stack. So we can't take anything for the middle, we can't take it to the bottom.
(00:46) All we can do is just add stuff to the top, take it off, in that order. Now in applications we have what we call the call stack. Now the call stack has a couple of responsibilities. The first is to keep track of the method that control should be returned to after the current method is finished executing.
(01:03) So whenever you call a method in your code, that's going to get added to the call stack and each subsequent method is just going to added to the call stack until they’ve finished executing. Now the second thing that the call stack is responsible for is for keeping track of the local variables in your method.
(01:16) In a minute, I’ll also to be covering where the other variables are stored in memory. Now the heap, unlike the stack, allows you to store items in memory in any order. You're not limited to take this top item from the stack. You can access it from anywhere. Now, with this added ability, it becomes a bit more complicated when it comes to adding removing items and therefore adding items to the heap has a higher overheads and it does adding items to the stack.
(01:38) Now generally the heap is used whenever you have data that's going to outlive what's on the call stack. So if you have a variable that needs to be accessed across different methods or some data that needs to be accessible, after a method has run then that is going to live on the heap. Now there are some general rules that you can apply to work out where your variables are stored in memory.
(01:55) In a previous video I talked about how we have value types and reference types. Now value types just store the value. So generally data types such as this, such as INT will be stored just as a single value of memory, whereas a reference type has a pointer to a value. So we have two parts to a reference type.
(02:12) We have a pointer which is generally just an address to a location in memory. Now the values for reference types are always stored on the heap. For value types, it really depends on where you declare it. Now, when you create a local variable in a method, we know that we can't access that variable from outside the method.
(02:27) And the reason for that is because that local variable is going to get stored on the call stack with the method. So soon as execution is finished, that block on the call stack is going to drop off and therefore that local variable isn't going to be available anymore. Now, if we were to declare a reference type variable inside your method, then it's just the pointer that stays on the call stack.
(02:47) The value for your reference type variable will be on the heap, but of course you can't access it because the pointer itself is gone once the method execution is finished. Now not all value types get stored on the stack. It really depends on where you declare it in your code. So if you have a global variable, then that's always going to be on the heap because you need to be able to access it from different areas in your code.
(03:06) And for example, if you create a class and then have variables inside your class, and therefore that class is also going to live on the heap and so the variables inside it. Now because our reference types only have a pointer to a location in memory, you have to wonder what happens to that block of memory after your method is finished executing? In most runtimes we have what we call a garbage collector.
(03:27) Now, as the name suggests, the garbage collector goes around collecting garbage. It goes around and looks at the heap and finds things aren't being used anymore and cleans them up. So this is how when a reference type thats declared in a method is no longer used, the pointer goes. And then because there's nothing pointing to it, the garbage collector comes, it cleans it up.
(03:44) So then the general rules you’ve got reference types always on the heap. Value types are either on the stack or the heap, depending on where there declared. Now of course, with all things, there are a few exceptions. Static variables, for example, will always be on the heap because you need to be able to access them from anywhere in your code.
(04:00) Now, in some languages such as C#, we have what we call anonymous functions. Now, as the name suggests, they don't have a name, but they can be created and called from inside another method. Now the thing with anonymous functions is they have access to the other variables that you declared in the method that called it.
(04:16) Now for the anonymous function to be able to read those variables, those variables need to at least temporarily be stored on the heap, as you can only ever read what's on the top of the stack. Of course, as soon as you call that anonymous function, it's going to go to the top of the stack and therefore not have access to anything that is on the previous level down.
(04:32) Now, once that anonymous function finishes, we're going to have a bunch of data that's just going to be dangling around and therefore the garbage collector is going to come clean it up. Now, the other exception is asynchronous methods. Now, whenever you run anything asynchronously, it's going to get run on a different thread.
(04:46) So when you're running your code, it has multiple threads and each thread has its own call stack. Now, because each thread is independent of one another, they can finish independently as well and therefore be able to access the results of your asynchronous methods. The result needs to be stored on the heap, so when you call your asynchronous method, that's going to go off on to a different thread.
(05:06) Your current thread is going to continue until you wait and get a result from the asynchronous method. And all of those results are just going to get stored in heaps that you can retrieve them later on. That's my quick overview of stack and heap. If you like this video, please make sure you like and subscribe and you might want to check out this one on Bitwise Operators and why we use them.
(05:23) Thank you for watching. I'll see you in the next video.

Created: 2024-02-12